
package de.uni.wuppertal.server.ui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.HyperlinkEvent.EventType;

import de.uni.wuppertal.server.ErrorLogEntry;
import de.uni.wuppertal.server.util.Resource;

/**
 * @author Martin Schröder
 * @author Patrik Nordsiek
 */
public class AboutDialog extends JDialog
{
	/**
	 * Serialisierung
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Panel für den Fensterinhalt.
	 */
	private JPanel mainPanel;
	
	/**
	 * Button zum Schließen.
	 */
	private JButton buttonClose;
	
	/**
	 * Anzeigefeld für Infos.
	 */
	private JEditorPane infoLabel;
	
	/**
	 * Hauptfenster.
	 */
	private MainWindow window;
	
	/**
	 * Erzeugt den Über-Dialog.
	 * 
	 * @param window Hauptfenster
	 */
	public AboutDialog(MainWindow window)
	{
		super(window, "Über", true);
		
		this.window = window;
		
		EventQueue.invokeLater(new Runnable()
		{
			@Override
			public void run()
			{
				setLocation(150, 150);
				setDefaultCloseOperation(DISPOSE_ON_CLOSE);
				setIconImage(Resource.getIcon("icon/server.png").getImage());
				
				mainPanel = new JPanel();
				mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
				mainPanel.setLayout(new BorderLayout(5, 5));
				//mainPanel.setOpaque(false);
				mainPanel.setBackground(null);
				add(mainPanel);
				
				createInfoLabel();
				createCloseButton();
				
				pack();
				
				setResizable(true);
			}
		});
	}
	
	/**
	 * Erstellt den Schließen-Button.
	 */
	private void createCloseButton()
	{
		buttonClose = new JButton("Schließen");
		
		buttonClose.setMnemonic('S');
		buttonClose.setToolTipText("Fenster schließen");
		
		buttonClose.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				dispose();
			}
		});
		
		getRootPane().setDefaultButton(buttonClose);
		
		mainPanel.add(buttonClose, BorderLayout.SOUTH);
	}
	
	/**
	 * Lädt den Text aus der Datei "resource/help/about.html".
	 */
	private void createInfoLabel()
	{
		try
		{
			infoLabel = new JEditorPane(Resource.getURL("about.html"));
			
			infoLabel.setEditable(false);
			infoLabel.setPreferredSize(new Dimension(400, 250));
			
			infoLabel.addHyperlinkListener(new HyperlinkListener()
			{
				@Override
				public void hyperlinkUpdate(HyperlinkEvent event)
				{
					try
					{
						if(event.getEventType() == EventType.ACTIVATED)
						{
							Desktop desktop = Desktop.getDesktop();
							
							if(desktop.isSupported(Desktop.Action.BROWSE))
							{
								desktop.browse(event.getURL().toURI());
							}
						}
					}
					catch(Exception e)
					{
						e.printStackTrace();
					}
				}
			});
			
			JPanel scrollPanel = new JPanel(new GridLayout(1, 1));
			
			scrollPanel.setBorder(BorderFactory.createTitledBorder(" Information "));
			
			JScrollPane infoScrollPane = new JScrollPane(infoLabel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
			
			scrollPanel.add(infoScrollPane);
			
			mainPanel.add(scrollPanel, BorderLayout.CENTER);
		}
		catch(Exception e)
		{
			window.log(new ErrorLogEntry(e));
		}
	}
}
